//程序所需要的函数
//包括：获取入场时间、获取出场时间、获取停车场信息、获取车辆信息、菜单选择
#include <iostream>
#include <ctime>
#include <algorithm>
#include <fstream>
#include "include.hpp"
using namespace std;

void carInf::getEntryTime() {
    cout << "请输入车牌号：" << endl;
    cin >> carNum;

    // 手动分配停车位
    int parkNum;
    cout << "请输入停车位编号：" << endl;
    cin >> parkNum;
    while (parkNum <= 0 || parkNum > PARKNUM || find(Parknums.begin(), Parknums.end(), parkNum) != Parknums.end()) {
        if (parkNum <= 0 || parkNum > PARKNUM) {
            cout << "停车位编号输入错误，请重新输入：" << endl;
        } else {
            cout << "停车位已被占用，请重新输入：" << endl;
        }
        cin >> parkNum;
    }
    Parknums.push_back(parkNum);

    // 获取当前时间
    time_t now = time(0);
    tm *ltm = localtime(&now);

    cout << "当前北京时间: ";
    cout << 1900 + ltm->tm_year << "年"
         << 1 + ltm->tm_mon << "月"
         << ltm->tm_mday << "日 "
         << ltm->tm_hour << "时"
         << ltm->tm_min << "分"
         << ltm->tm_sec << "秒" << endl;

    cout << carNum << " 您已成功入场" << endl;
    cout << "停车位编号: " << parkNum << endl;
    entrytime = now;

    // 打开car.csv，将车牌号、入场时间和停车位写入csv
    ofstream file("D:/parkinglot_management/car.csv", ios::app);
    if (file.is_open()) {
        file << carNum << ","
             << 1900 + ltm->tm_year << "年"
             << 1 + ltm->tm_mon << "月"
             << ltm->tm_mday << "日 "
             << ltm->tm_hour << "时"
             << ltm->tm_min << "分"
             << ltm->tm_sec << "秒,"
             << parkNum << endl;
        file.close();
    } else {
        cerr << "无法打开文件!" << endl;
    }
}



// 获取出场时间并删除对应的车牌号行
void carInf::getExitTime() {

    //输入车牌号
    cout << "请输入车牌号：" << endl;
    cin >> carNum;

    //获取当前时间
    time_t now = time(0);
    tm *ltm = localtime(&now);

    // 计算停车时间
    int time = difftime(now, entrytime);

    // 计算停车费用（前1个小时免费，之后每小时10元）
    int fee = 0;
    if (time > 3600) {
        fee = (time / 3600 - 1) * parkFeeper;
    }

    //输出当前时间
    cout << "当前北京时间: "
         << 1900 + ltm->tm_year << "年"
         << 1 + ltm->tm_mon << "月"
         << ltm->tm_mday << "日 "
         << ltm->tm_hour << "时"
         << ltm->tm_min << "分"
         << ltm->tm_sec << "秒" << endl;

    //输出停车时间（时分秒）
    cout << "停车时间: " 
         << time / 3600 
         << "小时"
         << time % 3600 / 60
         << "分钟"
         << time % 60
         << "秒"
         << endl;

    //输出停车费用
    cout << "停车费用: " << fee << "元" << endl;

    // 打开car.csv，将carNum对应的那一行删掉
    ifstream infile("D:/PARKINGLOT_MANAGEMENT/car.csv");
    ofstream tempFile("D:/PARKINGLOT_MANAGEMENT/temp.csv");
    string line;
    while (getline(infile, line)) {
        if (line.find(carNum) == string::npos) {
            tempFile << line << endl;
        }
    }
    infile.close();
    tempFile.close();

    // 删除原文件并重命名临时文件
    remove("D:/PARKINGLOT_MANAGEMENT/car.csv");
    rename("D:/PARKINGLOT_MANAGEMENT/temp.csv", "D:/PARKINGLOT_MANAGEMENT/car.csv");

    //出场成功
    cout << carNum << " 您已成功出场" << endl;
}

// 获取停车场信息并统计车辆数量
void carInf::getParkNum() {

    //打开car.csv
    ifstream file("D:/PARKINGLOT_MANAGEMENT/car.csv");


    if (file.is_open()) {
        string line;
        int vehicleCount = 0;
        bool isFirstLine = true;
        while (getline(file, line)) {
            if (isFirstLine) {
                isFirstLine = false; // 跳过表头行
                continue;
            }
            vehicleCount++;
            cout << line << endl;
        }
        file.close();
        cout << "当前停车场中的车辆数量: " << vehicleCount << endl;
        cout << "当前停车场中的剩余停车位数量: " << PARKNUM - vehicleCount << endl;
    } else {
        cerr << "无法打开CSV文件。" << endl;
    }
}

//输入车牌号，获取车辆信息
void carInf::getCarInf() {
    
    //输入车牌号
    string carNum;
    cout << "请输入车牌号：" << endl;
    cin >> carNum;

    //打开car.csv
    ifstream file("D:/PARKINGLOT_MANAGEMENT/car.csv");
    if (file.is_open()) {
        string line;
        bool isFirstLine = true;
        while (getline(file, line)) {
            //跳过表头行
            if (isFirstLine) {
                isFirstLine = false;
                continue;
            }
            //找到对应车牌号的行，并进行输出
            if (line.find(carNum) != string::npos) {
                cout << line << endl;
            }
        }
        file.close();
    } else {
        cerr << "无法打开CSV文件。" << endl;
    }
    
}

//自动分配停车位(从1到PARKNUM，随机分配一个car.csv中没有的停车位)
void carInf::autoPark() {
    //输入车牌号
    cout << "请输入车牌号：" << endl;
    cin >> carNum;

    //初始化随机数种子
    srand((unsigned)time(NULL));
    //生成一个1到PARKNUM的随机数，直到找到一个Parknums中没有的停车位
    int parkNum;
    do {
        parkNum = rand() % PARKNUM + 1;
    } while (find(Parknums.begin(), Parknums.end(), parkNum) != Parknums.end());
    //将parkNum加入Parknums
    Parknums.push_back(parkNum);

    // 获取当前时间
    time_t now = time(0);
    tm *ltm = localtime(&now);

    cout << "当前北京时间: ";
    cout << 1900 + ltm->tm_year << "年"
         << 1 + ltm->tm_mon << "月"
         << ltm->tm_mday << "日 "
         << ltm->tm_hour << "时"
         << ltm->tm_min << "分"
         << ltm->tm_sec << "秒" << endl;

    cout << carNum << " 您已成功入场" << endl;
    cout << "停车位编号: " << parkNum << endl;
    entrytime = now;

    // 打开car.csv，将车牌号、入场时间和停车位写入csv
    ofstream file("D:/parkinglot_management/car.csv", ios::app);
    if (file.is_open()) {
        file << carNum << ","
             << 1900 + ltm->tm_year << "年"
             << 1 + ltm->tm_mon << "月"
             << ltm->tm_mday << "日 "
             << ltm->tm_hour << "时"
             << ltm->tm_min << "分"
             << ltm->tm_sec << "秒,"
             << parkNum << endl;
        file.close();
    } else {
        cerr << "无法打开文件!" << endl;
    }
}
    

  
//菜单选择
int menu() {
    int choice;
    cout << "请输入功能编号: ";
    cin >> choice;
    return choice;
}